package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MagzineResult;
import com.lanou.dllo.findfood.util.Booklister;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class MagzineRecyclerAdpter extends RecyclerView.Adapter<MagzineRecyclerAdpter.RecyclerAdpter>{
    private List<MagzineResult> results;
    private Booklister booklister;
public  void  addData( List<MagzineResult> result){
    results = result;
    notifyDataSetChanged();

}
    public void setLister(Booklister book){

        booklister = book;
    }

    @Override
    public RecyclerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyler_magzin_item,null);
        return new RecyclerAdpter(view);
    }

    @Override
    public void onBindViewHolder(RecyclerAdpter holder, int position) {
      MagzineResult result = results.get(position);
       String url = result.getImg();
        String imgUrl = " http://tnfs.tngou.net/image/" + url;
            getBitmap(imgUrl,holder.BookImg);

          holder.position = position;
    }

    @Override
    public int getItemCount() {
        return results!= null && results.size() > 0?results .size() :0;
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }
    class RecyclerAdpter extends  RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView BookImg;
        private  int position;

        public RecyclerAdpter(View itemView) {
            super(itemView);
            BookImg = (NetworkImageView) itemView.findViewById(R.id.BookImg);
            BookImg.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.BookImg:
                    MagzineResult list = results.get(position);
                    if (booklister!= null){
                        booklister.bookClick(list);
                    }


                break;
                default:
                    break;
            }
        }
    }
}
